
var gameModel = require('../model/gameModel'),
    mditor = require('mditor'),
    // formidable = require('formidable'),
    fs = require('fs'),
    TITLE = '文件上传示例',
    // url = require('../common/url'),
    // url = 'http://www.gamesun.cn:3000/images/', //生产环境
    // url = 'http://127.0.0.1:3000/images/', //内网
    // maUrl = 'http://www.gamesun.cn/',// 生产环境

    multer = require('multer'),
    upload = multer({ dest: './public/images' }),
    //保存上传图片路径
    globalPath = ''
    // utlis = require('../common/utlis');

module.exports = {
    /**
        , *  加载页面
        , * @param {*} req 
        , * @param {*} res 
        , */
    uploadhtml(req, res) {
        res.render('img')// 显示上传页面ß
    }
    ,
    /**
     *      上传图片.
     * @param {*} req 
     * @param {*} res 
     */
    addimg(req, res) {
        console.log(req.file);
        // if(!req.file.originalname) {
        //     res.send('<script>window.parent.handleError("请选择上传文件");history.go(-1);</script>');
        // }
        var name, obj = {}
        obj.name = req.file.filename;


        res.send(obj)
        // res.render('img',obj)
    },
    showfile(req, res) {
        // console.log(req);

        var url = req.url;
        fs.readFile(url, 'utf-8', (err, data) => {
            if (err) return res.end('404');
            res.end(data);
        });
        // res.send(111);
    },
   
    getAllgame(req, res) {
        res.header("Access-Control-Allow-Origin", "*");

        var nowPage = req.query.nowPage
        var pageSize = req.query.pageSize
        // 
        gameModel.getAllgame(nowPage, pageSize, (err, results) => {
            if (err) return res.send({ 500: "服务器内部错误" })
            // res.send(JSON.stringify(results));
            // console.log(results);
            // console.log(url.server);÷
            results.forEach(element => {
                element.game_img1 = url + element.game_img1;
                element.game_img2 = url + element.game_img2;
                element.game_img3 = url + element.game_img3;
                element.game_img4 = url + element.game_img4;
                element.game_url_ma = url + element.game_url_ma
                switch (element.game_type) {
                    case 1:
                        element.game_type = "角色扮演"
                        break;
                    case 2:
                        element.game_type = "战争策略"
                        break;
                    case 3:
                        element.game_type = "竞技游戏"
                        break;
                    case 4:
                        element.game_type = "休闲游戏"
                        break;
                    case 5:
                        element.game_type = "模拟养成"
                        break;

                    default:
                        break;
                }
            });
            // res.header("Access-Control-Allow-Origin", "*");
            //   res.render("/game/list",{
            //   })
            console.log(results);

            res.send(results);
            // res.send("<img src='"+results[0].game_url +"'>");
        })
    },
    

    sort(req, res) {

        var obj = {};
        // obj.game_sort="recomend";
        // console.log(req.query);
        obj = req.query
        console.log(obj);
        res.header("Access-Control-Allow-Origin", "*");
        gameModel.sort(obj, (err, results) => {
            if (err) return res.send({ "err": "查询失败" });
            results.forEach(element => {
                element.game_img1 = url + element.game_img1;
                element.game_img2 = url + element.game_img2;
                element.game_img3 = url + element.game_img3;
                element.game_img4 = url + element.game_img4;
                element.game_url_ma = url + element.game_url_ma
                // var type=
                switch (element.game_type) {
                    case 1:
                        element.game_type = "角色扮演"
                        break;
                    case 2:
                        element.game_type = "战争策略"
                        break;
                    case 3:
                        element.game_type = "竞技游戏"
                        break;
                    case 4:
                        element.game_type = "休闲游戏"
                        break;
                    case 5:
                        element.game_type = "模拟养成"
                        break;

                    default:
                        break;
                }
            });
            if (results) return res.send({ "ok": 1, data: results });
        })
    },
   
    /**
     * 模糊查询游戏
     */
    fuzzyQuery(req, res) {
        res.header("Access-Control-Allow-Origin", "*");
        var strLike = req.query.en_name;
        gameModel.fuzzyQuery(strLike, (err, results) => {
            if (err) return res.send({ error: 0, msg: "查询失败" })
            return res.send(results);
        })
    },
   
    /**
     * 查询某个游戏
     */
    AGame(req, res) {
        res.header("Access-Control-Allow-Origin", "*");
        var obj = req.query;
        gameModel.sort(obj, (err, results) => {
            if (err) return res.send({ "err": 0, msg: "查询失败" });
            results.forEach(element => {
                element.game_img1 = url + element.game_img1;
                element.game_img2 = url + element.game_img2;
                element.game_img3 = url + element.game_img3;
                element.game_img4 = url + element.game_img4;
                element.game_url_ma = url + element.game_url_ma
                switch (element.game_type) {
                    case 1:
                        element.game_type = "角色扮演"
                        break;
                    case 2:
                        element.game_type = "战争策略"
                        break;
                    case 3:
                        element.game_type = "竞技游戏"
                        break;
                    case 4:
                        element.game_type = "休闲游戏"
                        break;
                    case 5:
                        element.game_type = "模拟养成"
                        break;

                    default:
                        break;
                }
            });
            res.send({ "ok": 1, data: results });
        })
    },

    /**
     * 获取有轮播图的游戏
     * @param {*} req 
     * @param {*} res 
     */
    ISOut(req,res){
        res.header("Access-Control-Allow-Origin", "*");
        var str= req.query.isOut;
        console.log(str);
          
        gameModel.ISOut(str,(err,results)=>{
            if (err) return res.send({ error: 0, msg: "查询失败" });
            results.forEach(element=>{
                // element.game_url= maUrl+element.game_url;
                // element.lunbo_img_pc=url+ element.lunbo_img_pc;
            })
            res.send(results);
        });
    }
}